package br.mikhas.util;

/**
 * Provides String utilities
 * 
 * @author Mikhail Domanoski
 * 
 */
public final class StringUtils {
	private StringUtils() {
	}

	/**
	 * Capitalizes the first letter of a String.
	 * <p>
	 * Ex.: lower case => Lower case
	 */
	public static String capitalize(String string) {
		Assert.notEmpty(string);
		char[] charArray = string.toCharArray();
		charArray[0] = Character.toUpperCase(charArray[0]);
		return new String(charArray);
	}

	/**
	 * Uncapitalizes the first letter of a String
	 * <p>
	 * Ex.: UPPER CASE => uPPER CASE
	 */
	public static String uncapitalize(String string) {
		Assert.notEmpty(string);
		char[] charArray = string.toCharArray();
		charArray[0] = Character.toLowerCase(charArray[0]);
		return new String(charArray);
	}

	/**
	 * Capitalizes the first letter of all words on the string
	 * <p>
	 * Ex.: lower case => Lower Case
	 */
	public static String capitalizeAll(String string) {
		Assert.notEmpty(string);
		char[] charArray = string.toCharArray();
		charArray[0] = Character.toUpperCase(charArray[0]);

		if (charArray.length > 1) {

			boolean lastBlank = false;
			char c;
			for (int i = 1; i < charArray.length; i++) {
				c = charArray[i];

				if (c == ' ' || c == '\n') {
					lastBlank = true;
				} else {
					if (lastBlank) {
						charArray[i] = Character.toUpperCase(c);
					}

					lastBlank = false;
				}
			}
		}
		return new String(charArray);
	}

	public static boolean isEmpty(String string) {
		return string == null || string.isEmpty();
	}
}
